package com.sky.controller.admin;

import com.sky.result.Result;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.InputStreamResource;
import org.springframework.core.io.Resource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.time.LocalDate;

/**
 * 数据统计相关接口
 */
@RestController
@Slf4j
@RequestMapping("/admin/report")
public class ReportController {
  
  @Autowired
  private ReportService reportService;
  
  /**
   * 营业额统计
   * @return
   */
  @GetMapping("/turnoverStatistics")
  public Result<TurnoverReportVO> turnoverStatistics(
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate begin,
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate end
  ) {
    log.info("begin: {}, end: {}", begin, end);
    TurnoverReportVO reportVO = reportService.turnoverStatistics(begin, end);
    return Result.success(reportVO);
  }
  
  /**
   * 用户统计
   * @return
   */
  @GetMapping("/userStatistics")
  public Result<UserReportVO> userStatistics(
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate begin,
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate end
  ) {
    UserReportVO userReportVO = reportService.userStatistics(begin, end);
    return Result.success(userReportVO);
  }
  
  /**
   * 订单统计
   * @param begin
   * @param end
   */
  @GetMapping("/ordersStatistics")
  public Result<OrderReportVO> orderStatistics(
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate begin,
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate end
  ) {
    OrderReportVO orderReportVO = reportService.orderStatistics(begin, end);
    return Result.success(orderReportVO);
  }
  
  /**
   * 销量top10
   */
  @GetMapping("/top10")
  public Result<SalesTop10ReportVO> SalesTop10(
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate begin,
      @DateTimeFormat(pattern = "yyyy-MM-dd") @RequestParam LocalDate end
  ) {
    return Result.success(reportService.SalesTop10ReportVO(begin, end));
  }
  
  /**
   * 数据导出
   */
  @GetMapping("/export")
  public void export(HttpServletResponse response) throws Exception {
    reportService.exportBusinessData(response);
    
  }
}
